package com.cyk.test6.domain.vo

import net.sf.jsqlparser.util.validation.ValidationException

data class Name(
    val name: String?
) {
    init {
        if(name.isNullOrBlank()) {
            throw ValidationException("name")
        }
    }
}

data class Address (
    val address: String
)

data class Phone(
    val phone: String?
) {
    init {
        if(phone.isNullOrBlank()) {
            throw ValidationException("phone 不能为空")
        }
        if(!isValidPhoneNumber(phone)) {
            throw ValidationException("phone 格式错误")
        }
    }

    fun getAreaCode(): String? {
        phone?.let {
            val areas = arrayOf("0571", "021", "010")
            for (i in it.indices) {
                val prefix: String = it.substring(0, i)
                if (areas.contains(prefix)) {
                    return prefix
                }
            }
        }
        return null
    }

    private fun isValidPhoneNumber(phone: String): Boolean {
        val pattern = "^0[1-9]{2,3}-?\\d{8}$".toRegex()
        return pattern.matches(phone)
    }

}